package com.example.testapplication.ui.home.home_list_fragment;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.example.testapplication.BR;
import com.example.testapplication.R;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

public class HomeListFragViewModel extends BaseViewModel {
    private static final String MultiRecycleType_Head = "head";
    private static final String MultiRecycleType_A = "typeA";
    private static final String MultiRecycleType_B = "typeB";

    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //下拉刷新完成
        public SingleLiveEvent finishRefreshing = new SingleLiveEvent<>();
        //上拉加载完成
        public SingleLiveEvent finishLoadmore = new SingleLiveEvent<>();
    }


    public HomeListFragViewModel(@NonNull Application application) {
        super(application);
        //模拟10个条目，数据源可以来自网络
        for (int i = 0; i < 20; i++) {
            if (i == 0) {
                MultiItemViewModel item = new MutiItemHeadViewModel(this);
                //条目类型为头布局
                item.multiItemType(MultiRecycleType_Head);
                observableList.add(item);
            } else {
                String text = "我是第" + i + "条";
                if (i % 2 == 0) {
                    MultiItemViewModel item = new MutiItemTypeAViewModel(this, text);
                    //条目类型为左布局
                    item.multiItemType(MultiRecycleType_A);
                    observableList.add(item);
                } else {
                    MultiItemViewModel item = new MutiItemTypeBViewModel(this, text);
                    //条目类型为右布局
                    item.multiItemType(MultiRecycleType_B);
                    observableList.add(item);
                }
            }
        }
    }

    //给RecyclerView添加ObservableList
    public ObservableList<MultiItemViewModel> observableList = new ObservableArrayList<>();
    //RecyclerView多布局添加ItemBinding
    public ItemBinding<MultiItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            //通过item的类型, 动态设置Item加载的布局
            String itemType = (String) item.getItemType();
            if (MultiRecycleType_Head.equals(itemType)) {
                //设置头布局
                itemBinding.set(BR.viewModel, R.layout.item_multi_head);
            } else if (MultiRecycleType_A.equals(itemType)) {
                //设置左布局
                itemBinding.set(BR.viewModel, R.layout.item_multi_rv_left);
            } else if (MultiRecycleType_B.equals(itemType)) {
                //设置右布局
                itemBinding.set(BR.viewModel, R.layout.item_multi_rv_right);
            }
        }
    });


    public BindingCommand onRefreshCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            for (int i = 0; i < 5; i++) {
                MultiItemViewModel viewModel;
                if(i%2==0){
                    viewModel = new MutiItemTypeAViewModel(HomeListFragViewModel.this, "下拉刷新"+i);
                    viewModel.multiItemType(MultiRecycleType_A);
                }else {
                    viewModel = new MutiItemTypeBViewModel(HomeListFragViewModel.this, "下拉刷新"+i);
                    viewModel.multiItemType(MultiRecycleType_B);
                }
               observableList.add(0,viewModel );
            }
            uc.finishRefreshing.call();
        }
    });

    public BindingCommand onLoadmoreCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            for (int i = 0; i < 5; i++) {
                MultiItemViewModel viewModel;
                if(i%2==0){
                    viewModel = new MutiItemTypeAViewModel(HomeListFragViewModel.this, "上拉加载"+i);
                    viewModel.multiItemType(MultiRecycleType_A);
                }else {
                    viewModel = new MutiItemTypeBViewModel(HomeListFragViewModel.this, "上拉加载"+i);
                    viewModel.multiItemType(MultiRecycleType_B);
                }
                observableList.add(viewModel );
            }
            uc.finishLoadmore.call();
        }
    });
}
